package cn.bdqn.witcity.controller;

import cn.bdqn.witcity.config.SendQueue;
import cn.bdqn.witcity.entity.Comment;
import cn.bdqn.witcity.entity.Order;
import cn.bdqn.witcity.entity.Product;
import cn.bdqn.witcity.entity.User;
import cn.bdqn.witcity.entity.vo.OrderVo;
import cn.bdqn.witcity.service.CommentService;
import cn.bdqn.witcity.service.OrderService;
import cn.bdqn.witcity.service.ProductService;
import cn.bdqn.witcity.util.ResponseResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {
    @Resource
    SendQueue sendQueue;
    @Resource
    OrderService orderService;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    ProductService productService;
    @Resource
    CommentService commentService;


    @RequestMapping("/order/create")
    @ResponseBody
    public ResponseResult createOrder(Integer num, Integer pid, Authentication authentication){
        User user=(User)authentication.getPrincipal();
        Product product=productService.findProductById(pid);
        String stock=(String)redisTemplate.opsForValue().get(pid.toString());
        if(num>Integer.parseInt(stock)){
            return new ResponseResult(100,"库存数量不足",null);
        }
        if(user.getMoney()<product.getPrice()*num){
            return new ResponseResult(120,"你的余额不足",null);
        }
        Order order=new Order();
        order.setPrice(product.getPrice()*num);
        order.setProductNum(num);
        order.setShoppingId(product.getShopId());
        order.setUid(user.getId());
        order.setProductName(product.getProductName());
        int result=orderService.addOrder(order);
        if(result>0){
            Map<String,Object> map=new HashMap<>();
            map.put("pid",pid);
            map.put("order",order);
            sendQueue.send(map);
            return new ResponseResult(0,"下单成功",result);
        }
        return new ResponseResult(500,"发生异常",result);
    }
    @RequestMapping("/sys/order")
    @ResponseBody
    public ResponseResult sysOrderList(){
        List<OrderVo> orderVos=orderService.findAll();
        int count=orderService.getAllCount();
        return new ResponseResult(0,"",orderVos,count);
    }
    @RequestMapping("/yezhu/meorder")
    @ResponseBody
    public ResponseResult yuOrder(Authentication authentication){
        User user=(User)authentication.getPrincipal();
        List<OrderVo> orderVos=orderService.findOrderById(user.getId());
        int count=orderService.findCountById(user.getId());
        return new ResponseResult(0,"",orderVos,count);
    }
    @RequestMapping("/addComment")
    @ResponseBody
    public ResponseResult addComment(Integer id,String content){
        Comment comment=new Comment();
        comment.setOrderId(id);
        comment.setContent(content);
        int result=commentService.addComment(comment);
        if(result>0){
            return new ResponseResult(200,"",result);
        }
        return new ResponseResult(0,"",result);
    }
}
